# app/ui/utils.py - UI工具函数
from PySide6.QtGui import QImage, QPixmap
from PySide6.QtCore import Qt
import os

def convert_cv_to_pixmap(cv_img):
    """将OpenCV图像转换为Qt的Pixmap"""
    height, width, channel = cv_img.shape
    bytes_per_line = 3 * width
    q_image = QImage(cv_img.data, width, height, bytes_per_line, QImage.Format_RGB888)
    q_image = q_image.rgbSwapped()
    return QPixmap.fromImage(q_image)

def format_class_counts(class_counts):
    """格式化类别计数为HTML文本"""
    return "<br>".join([f"{class_name}: {count}" 
                       for class_name, count in sorted(class_counts.items(), 
                                                      key=lambda x: x[1], 
                                                      reverse=True)
                       if count > 0])

def load_image(image_path):
    """加载图片并返回QPixmap对象"""
    if os.path.exists(image_path):
        pixmap = QPixmap(image_path)
        if not pixmap.isNull():
            return pixmap
    return None